new Vue({
    el:"#top",
    template:"<nav class=\"navbar navbar-expand-lg navbar-light bg-light\">\n" +
        "  <a class=\"navbar-brand\" href=\"#\">首页</a>\n" +
        "  <button class=\"navbar-toggler\" type=\"button\" data-toggle=\"collapse\" data-target=\"#navbarSupportedContent\" aria-controls=\"navbarSupportedContent\" aria-expanded=\"false\" aria-label=\"Toggle navigation\">\n" +
        "    <span class=\"navbar-toggler-icon\"></span>\n" +
        "  </button>\n" +
        "\n" +
        "  <div class=\"collapse navbar-collapse\" id=\"navbarSupportedContent\">\n" +
        "    <ul class=\"navbar-nav mr-auto\">\n" +
        "      <li class=\"nav-item active\">\n" +
        "        <a class='nav-link' href='#' v-if='is_login' :title=\"hi+','+username\">{{hi}},{{username}}</a>" +
        "        <a class=\"nav-link\" href=\"../html/login.html\" v-else>请登录</a>" +
        "      </li>\n" +
        "      <li class=\"nav-item\">\n" +
        "        <a class=\"nav-link\" href=\"#\">Link</a>\n" +
        "      </li>\n" +
        "      <li class=\"nav-item dropdown\">\n" +
        "        <a class=\"nav-link dropdown-toggle\" href=\"#\" id=\"navbarDropdown\" role=\"button\" data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"false\">" +
        "          <span>选项</span>" +
        "        </a>" +
        "        <div class=\"dropdown-menu\" aria-labelledby=\"navbarDropdown\">" +
        "          <a class=\"dropdown-item\" :href='permission.path' v-for='(permission,index) of permission' v-bind:key='permission.index'>{{permission.permission}}</a>" +
        "          <div class=\"dropdown-divider\"></div>" +
        "          <a class=\"dropdown-item\" href=\"#\">关于我们</a>" +
        "        </div>" +
        "      </li>\n" +
        "      <li class=\"nav-item\">\n" +
        "        <a class=\"nav-link\" href=\"#\" v-if='is_login' @click.prevent='logout' title='退出登录'>退出</a>" +
        "      </li>\n" +
        "    </ul>\n" +
        "  </div>\n" +
        "</nav>",
    data(){
        return {
            is_login:false,
            username:null,
            hi:"Hi",
            permission:[{permission:"未登录",path:"#"}]
        }
    },
    methods:{
        //登出
        logout:function(){
            this.clearCookie();
            location.reload();
        },

        //设置cookie
        setCookie: function (cname, cvalue, exdays) {
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toUTCString();
            console.info(cname + "=" + cvalue + "; " + expires);
            document.cookie = cname + "=" + cvalue + "; " + expires;
            console.info(document.cookie);
        },

        //获取cookie
        getCookie: function (cname) {
            var name = cname + "=";
            var ca = document.cookie.split(';');
            console.log("获取cookie,现在循环");
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                console.log(c);
                while (c.charAt(0) == ' ') c = c.substring(1);
                if (c.indexOf(name) != -1){
                    return c.substring(name.length, c.length);
                }
            }
            return "";
        },

        //清除cookie
        clearCookie: function () {
            this.setCookie("username", "", -1);
            this.setCookie("password","",-1);
        },

        //检查cookie
        checkCookie: function () {
            var user = this.getCookie("username");
            if (user != "") {
                this.changeIsLogin(true);
                this.changeUsername(user);
                this.getPermission(user);
            } else {

            }
        },

        //页面加载时调用
        created() {
            //查询登录状态
            this.checkCookie();
            //获取时间
            var hour = new Date().getHours();
            if(hour >= 6 && hour <= 7 ){
                this.hi = "早上好";
            }else if(hour >= 8 && hour < 12){
                this.hi = "上午好";
            }else if(hour >= 12 && hour < 14){
                this.hi = "中午好";
            }else if(hour >= 14 && hour < 18){
                this.hi = "下午好";
            }else if(hour >= 18 || hour < 6){
                this.hi = "晚上好";
            }
        },

        //改变登陆状态
        changeUsername(user){
            this.username = user;
        },
        changeIsLogin(boolean){
            this.is_login = boolean;
        },

        //获取权限列表
        getPermission:function(username){
            var that = this;
            axios.get('http://134.175.47.41:8083/pokegogogo/user/getPermissionByUsername?username='+username)
                .then(function (response) {
                    if(response.data.success){
                        that.permission = response.data.permission;
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
        }
    },

    mounted(){
    this.created();

}

});
